﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ridofy.LastFm;
using Ridofy.Spotify;

namespace Ridofy
{
    public class CustomLibrary
    {

        public void PlayLovedTracks()
        {
            Track[] tracks = new LastFmRssClient().RecentTracks("antolingarcia");
            foreach (var item in tracks)
            {
                Console.WriteLine("Buscando " + item.Title + " de " + item.Artist.Name);
                SpotifySearchResult spoList = new SpotifyClient().SearchTrack(item.Title);

                if (spoList.Tracks == null)
                {
                    Console.WriteLine(item.Title + " not found in spoti");
                }
                else
                {
                    IEnumerable<Track> result = from s in spoList.Tracks
                                                where CompareArtists(item, s)
                                                select s;

                    if (result.Count() < 1)
                    {
                        Console.WriteLine("no se ha encontrado la cancion del artista");
                        AskToPlaySpotifyItem(spoList.Tracks);
                    }
                    else
                    {
                        AskToPlaySpotifyItem(result);
                    }
                }
            }
        }

        private static bool CompareArtists(Track item, Track s)
        {            
            bool result =  s.Artist.Name.ToLowerInvariant().Contains(item.Artist.Name.ToLowerInvariant().Substring(4, item.Artist.Name.Length-5));
            //Console.WriteLine(string.Format("comparando '{0}' con '{1}' resultado: {2}",  
            //                        item.Artist.Name.ToLowerInvariant() , 
            //                        s.Artist.Name.ToLowerInvariant(), 
            //                        result.ToString()));
            return result;
            
        }

        public void PrepareTracks()
        {

            List<Track> result = new List<Track>();
            LastFmClient lfm = new LastFmClient();
            SpotifyClient spo = new SpotifyClient();
            LastFmResponse<LovedTrackList> lovedResult = lfm.UserGetLovedTracks("ridomin");



            foreach (var item in lovedResult.Content.Tracks)
            {
                Console.WriteLine("Select {0} {1} {2}", item.Title, item.Artist.Name, item.Album.Name);
                var todo = Console.ReadLine();
                if (todo == "y")
                {
                    Console.WriteLine("Looking in Spotify");
                    SpotifySearchResult spoList = spo.SearchTrack(item.Title);     
                    var res = from s in spoList.Tracks
                                    where s.Artist.Name.Contains(item.Artist.Name)
                                    select s;
                    AskToPlaySpotifyItem(res);
                    
                }
            }
        }

        private void AskToPlaySpotifyItem(IEnumerable<Track> tracks)
        {
                            
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Spotify FOUND " + tracks.Count());

            foreach (var s in tracks)
            {
                Console.WriteLine("Play: {0} :: {1} :: {2} ? y|n|q ", s.Title, s.Artist.Name, s.Album.Name);
                var action = Console.ReadLine();
                if (action == "y")
                {
                    System.Diagnostics.Process.Start(s.SpotifyUrl);

                }
                if (action == "q")
                {
                    break;
                }
                
            }                                 
        }
    }
}
